package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.pojo.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 仲夏夜
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Slf4j
public class EduChapterController {
    @Resource
    private EduChapterService eduChapterService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) throws Exception {
        return R.ok().data("allChapterVideo", eduChapterService.getChapterVideoByCourseId(courseId));
    }

    /**
     * 添加章节
     *
     * @param eduChapter
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) throws Exception {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 根据章节id查询
     *
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) throws Exception {
        return R.ok().data("chapter", eduChapterService.getById(chapterId));
    }

    /**
     * 修改章节
     *
     * @param eduChapter
     * @return
     */
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) throws Exception {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 删除的方法
     *
     * @param chapterId
     * @return
     */
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) throws Exception {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }
}